//app.js
import api from 'api/index'
import {userInfoStore} from 'store/userInfo'

App({
  onLaunch: function (options) {
    console.log('App onLaunch', options)
    
    this.globalData.path = options.path;
    this.globalData.query = options.query;
    this.globalData.scene = options.scene;
    let { scene } = options.query;
    if (scene) {/*小程序码过来  pages/index?scene=xxxx*/
      this.globalData.inviteCode = scene;
      console.log('App onLaunch', this.globalData)
    }
    let self = this;
    wx.checkSession({
      success() {
        //session_key 未过期，并且在本生命周期一直有效
        console.log('session_key 未过期，并且在本生命周期一直有效');
        if (!userInfoStore.userInfo.openId) {
          self.wxlogin();
        } 
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        console.log('session_key 已经失效，需要重新执行登录流程');
        self.wxlogin();
      }
    })

  },

  onHide(){
  },

  onShow: function (options) {
    if (options.scene && options.path.length>0){
      console.log('App onShow', options)

      this.globalData.path = options.path;
      this.globalData.query = options.query;
      this.globalData.scene = options.scene;

      let { scene } = options.query;

      /*扫小程序码过来,打开页面是pages/index?scene=xxxxxx  */
      if (scene) this.globalData.inviteCode = scene; 

      if (userInfoStore.userInfo.openId) {
          //  this.getUserInfo();
          //  this.addUserVisitPage();
      }
    }
    let self = this;
    let menuButtonClientRect = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight,
          navigationBarHeight = statusBarHeight + menuButtonClientRect.height + (menuButtonClientRect.top - statusBarHeight) * 2;//导航高度

        self.globalData.menuButtonClientRect = menuButtonClientRect;
        self.globalData.navigationBarClientRect = { width: res.windowWidth, height: navigationBarHeight };
        self.globalData.windowClientRect = { width: res.windowWidth, height: res.windowHeight };
      }
    })

    
  },

  globalData: {
    path:'',
    query:'',
    scene:'',
    inviteCode:null,
    menuButtonClientRect: null,
    navigationBarClientRect: null,
    windowClientRect:null,
    isPop:false,
    clickMeNav:null,
  },

  // share:function(){
  //   let param = userInfoStore.userInfo.recomendCode;
  //   return {
  //     title: '大金舒适家',
  //     path: 'pages/index?scene=' + param ,
  //     success: function (res) { }
  //   }
  // },

  displayGiftBag(component) {
    if (userInfoStore.userInfo.openId){
      api.getStatus({
        openId: userInfoStore.userInfo.openId,
      }).then(res => {
        component.show();
      },err => {
        component.hide();
      })
    }
  },

  addUserVisitPage() {
    let param = {
      openId: userInfoStore.userInfo.openId,
      path: this.globalData.path,
      query: JSON.stringify(this.globalData.query),
      scene: this.globalData.scene
    }
    api.addUserVisitPage(param).then(res => {
    });
  },

  getUserInfo: function () {
    var param = { openId: userInfoStore.userInfo.openId }
    if (this.globalData.inviteCode)
      param = Object.assign(param, { recomendCode: this.globalData.inviteCode })

      api.getUser(param).then(res => {
        let { userInfo } = userInfoStore;
        let { result } = res;
        result.session_key = userInfo.session_key;
        if (result.status == 1) result.level = -1;
        userInfoStore.updateUserInfo(result);

      })
  },

  wxlogin: function (){
    let self=this;
    wx.login({
      success(data) {
        console.log('wx.login 接口调用成功, code=', data.code);
        var param = { code: data.code }
        if (self.globalData.inviteCode)
        param = Object.assign(param, { recomendCode: self.globalData.inviteCode })
        
        api.initUser(param).then(res => {
          userInfoStore.updateUserInfo(res.result);

          self.addUserVisitPage();
        });
      },
      fail(err) {
        console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
      }
    })
  }

})
